package cn.wh.service.impl;

import cn.idworker.Sid;
import cn.wh.enums.MsgActionEnum;
import cn.wh.enums.MsgSignFlagEnum;
import cn.wh.mapper.*;
import cn.wh.netty.pojo.Msg;
import cn.wh.pojo.ChatMsg;
import cn.wh.pojo.FriendsRequest;
import cn.wh.pojo.MyFriends;
import cn.wh.pojo.User;
import cn.wh.service.UserServer;
import cn.wh.utils.CodeUtils;
import cn.wh.utils.FastDFSClient;
import cn.wh.utils.FileUtils;
import cn.wh.utils.MD5Utils;
import cn.wh.vo.MyFriendsVO;
import cn.wh.vo.UserVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2021/2/4 18:39
 */
@Service
public class UserServerImpl implements UserServer {

    @Autowired
    UserMapper userMapper;

    @Autowired
    Sid sid;

    @Autowired
    UserMapperCustom userMapperCustom;

    @Autowired
    CodeUtils codeUtils;

    @Autowired
    FastDFSClient fastDFSClient;

    @Autowired
    MyFriendsMapper myFriendsMapper;

    @Autowired
    FriendsRequestMapper friendsRequestMapper;

    @Autowired
    ChatMsgMapper chatMsgMapper;

    @Override
    public User getUserById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User queryUserNameIsExit(String username) {
        return userMapper.queryUserNameIsExit(username);
    }

    @Override
    public User insert(User user) {
        String SId = sid.nextShort();
        //为每一个用户生成一个唯一的二维码
        String qrCodePath = "D://" + SId + "qrcode.png";
        //创建二维码对象信息
        codeUtils.createQRCode(qrCodePath, "wh_qrcode" + user.getUsername());
        MultipartFile qrCodeFile = FileUtils.fileToMultipart(qrCodePath);
        String qrCodeURL = null;
        try {
            qrCodeURL = fastDFSClient.uploadQRCode(qrCodeFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        user.setNickname(user.getUsername());
        user.setPassword(MD5Utils.getPwd(user.getPassword()));
        user.setQrcode("/" + qrCodeURL);
        user.setFaceImage("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3456679434,2428723502&fm=26&gp=0.jpg");
        user.setFaceImageBig("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3456679434,2428723502&fm=26&gp=0.jpg");
        user.setId(SId);
        userMapper.insert(user);
        //删除本地二维码
        File file = new File(qrCodePath);
        file.delete();
        return user;
    }

    @Override
    public User updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
        return getUserById(user.getId());
    }

    @Override
    public UserVo queryAllUser(String username) {
        UserVo userVo = null;
        User userResult = userMapper.queryUserNameIsExit(username);
        if (userResult != null) {
            userVo = new UserVo();
            BeanUtils.copyProperties(userResult, userVo);
        }
        return userVo;
    }

    @Override
    public int queryToMyFriend(String sendUserId, String acceptUserId) {
        int i = 0;
        if (myFriendsMapper.queryToMyFriend(sendUserId, acceptUserId) != null) {
            i = 1;
        }
        return i;
    }

    //发送好友请求
    @Override
    public int sendFriendRequest(String sendUserId, String acceptUserId) {
        FriendsRequest myFriendsRequest = new FriendsRequest();
        myFriendsRequest.setId(sid.nextShort());
        myFriendsRequest.setSendUserId(sendUserId);
        myFriendsRequest.setAcceptUserId(acceptUserId);
        myFriendsRequest.setRequestDateTime(new Date());
        return friendsRequestMapper.insert(myFriendsRequest);
    }

    //查询是否发送过好友请求
    @Override
    public int isSendFriendRequest(String sendUserId, String acceptUserId) {
        int i;
        FriendsRequest sendFriendRequest = friendsRequestMapper.isSendFriendRequest(sendUserId, acceptUserId);
        if (sendFriendRequest != null) {
            i = 1;
        } else {
            i = 0;
        }
        return i;
    }

    @Override
    public int queryRequestMsgCount(String id) {
        return friendsRequestMapper.queryRequestMsgCount(id);
    }

    @Override
    public List<UserVo> queryRequestMsgList(String id) {
        //查看请求消息
        List<UserVo> userVoList = new ArrayList<>();
        List<FriendsRequest> friendsRequestList = friendsRequestMapper.queryRequestMsgList(id);
        for (FriendsRequest friendsRequest : friendsRequestList) {
            String sendUserId = friendsRequest.getSendUserId();
            //获取发送请求的用户数据
            User user = userMapper.selectByPrimaryKey(sendUserId);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(user, userVo);
            userVoList.add(userVo);
        }
        return userVoList;
    }

    @Override
    public void deleteFriendRequest(FriendsRequest friendsRequest) {
        friendsRequestMapper.deleteRequestMsg(friendsRequest);
    }

    @Override
    public void insertFriendInfo(String accpetUserId, String sendUserId) {
        saveFriend(accpetUserId, sendUserId);
        saveFriend(sendUserId, accpetUserId);
    }

    @Override
    public String saveMsg(Msg msg) {
        ChatMsg chatMsg = new ChatMsg();
        String msgId = sid.nextShort();
        String sendId = msg.getSendId();
        String receiveId = msg.getReceiveId();
        String content = msg.getContent();
        Integer type = msg.getType();
        chatMsg.setId(msgId);
        chatMsg.setCreateTime(new Date());
        chatMsg.setSignFlag(MsgSignFlagEnum.unsign.type);
        chatMsg.setAcceptUserId(receiveId);
        chatMsg.setMsg(content);
        chatMsg.setSendUserId(sendId);
        chatMsg.setType(type);
        chatMsgMapper.insert(chatMsg);
        return msgId;
    }

    @Override
    public void signMessage(String msgId) {
        ChatMsg chatMsg = new ChatMsg();
        chatMsg.setId(msgId);
        chatMsg.setSignFlag(MsgSignFlagEnum.signed.type);
        chatMsgMapper.update(chatMsg);
    }

    @Override
    public List<ChatMsg> queryUnSignMsg(String myId) {
        return chatMsgMapper.queryUnSignMsg(myId);
    }

    public void saveFriend(String accpetUserId, String sendUserId) {
        MyFriends myFriends = new MyFriends();
        myFriends.setId(sid.nextShort());
        myFriends.setMyUserId(accpetUserId);
        myFriends.setMyFriendUserId(sendUserId);
        myFriendsMapper.insert(myFriends);
    }


    @Override
    public List<MyFriendsVO> queryMyFriends(String userId) {
        return userMapperCustom.queryMyFriends(userId);
    }





}
